<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Reimburse;

use App\Func\UploadFile;
use App\Models\Reimburse\ReimburseApply;
use App\Models\Reimburse\ReimbursePaid;
use App\Models\Reimburse\ReimburseDetail;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PaidAjax {

    public function paidFile($id){
        $rp = ReimbursePaid::query()->find($id);
        $paidStatus = $rp->paid_status;
        $file = $rp->paid_file ? [$rp->paid_file] : [];
        $initialPreview = $initialPreviewConfig = [];
        $file = json_encode($file);
        if($file){
            $upload = UploadFile::getInit();
            $initialPreview = $upload->initialPreviewUploadFile($file);
            $initialPreviewConfig = $upload->initialPreviewConfigUploadFile($file);
        }
        return view('reimburse.paid.files-list',compact('id','paidStatus','initialPreview','initialPreviewConfig'));
    }

    /**
     * 返款支付
     * @param UploadedFile $file
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function paid(UploadedFile $file,$id){
        try{
            if ($file->isValid()) {
                DB::beginTransaction();
                $uploadFile = UploadFile::getInit();
                $path = $uploadFile->upload($file,'reimburse');
                $date = date('Y-m-d H:i:s');

                //返款支付
                $rp = ReimbursePaid::query()->find($id);
                if($rp->audit_status != 2){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '未审核！']);
                }
                if($rp->paid_status != 1){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '状态有误！']);
                }
                $rp->paid_status = 2;
                $rp->paid_date = $date;
                $rp->paid_file = $path;
                $flag = $rp->save();
                if(!$flag){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '操作失败！']);
                }
                $detailPaidStatus = ReimbursePaid::query()->where('reimburse_no',$rp->reimburse_no)->pluck('paid_status')->toArray();
                $paidStatus = ReimbursePaid::extractStatus($detailPaidStatus);

                //返款支付申请（审核）
                $ra = ReimburseApply::query()->where('reimburse_no',$rp->reimburse_no)->first();
                $ra->paid_status = $paidStatus;
                $flag1 = $ra->save();
                if(!$flag1){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '操作失败！']);
                }

                //返款支付明细
                $rd = ReimburseDetail::query()->where('reimburse_no',$rp->reimburse_no)->first();
                $rd->paid_status = $paidStatus;
                $rd->report_date = date('Y-m-01',strtotime($date));
                $rd->{'paid_amount_'.$rp->order_by} = $rp->paid_amount;
                if($rp->order_by != 4){
                    $rd->paid_date = $date;
                }else{
                    $rd->paid_date_4 = $date;
                    $rd->paid_method_4 = $rp->paid_method;
                    $rd->currency_code_4 = $rp->currency_code;
                    $rd->paid_account_no_4 = $rp->paid_account_no;
                }
                $flag2 = $rd->save();
                if(!$flag2){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '操作失败！']);
                }else{
                    DB::commit();
                    return response()->json(['status' => true, 'message' => '操作成功！']);
                }
            }else{
                return response()->json(['status' => false, 'message' => '无效文件']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '操作失败！']);
        }
    }

    public function paidChange(UploadedFile $file,$id){
        try{
            if ($file->isValid()) {
                DB::beginTransaction();
                $uploadFile = UploadFile::getInit();
                $path = $uploadFile->upload($file,'reimburse');
                $rp = ReimbursePaid::query()->find($id);
                $rp->paid_file = $path;
                $flag = $rp->save();
                if($flag){
                    DB::commit();
                    return response()->json(['status' => true, 'message' => '操作成功！']);
                }else{
                    DB::rollBack();
                    return response()->json(['status' => true, 'message' => '操作失败！']);
                }
            }else{
                return response()->json(['status' => false, 'message' => '无效文件']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误！']);
        }
    }

    public function batchPaidFile($ids){
        return view('reimburse.paid.files',compact('ids'));
    }

    public function batchPaid(UploadedFile $file,$ids){
        try{
            if ($file->isValid()) {
                DB::beginTransaction();
                $uploadFile = UploadFile::getInit();
                $path = $uploadFile->upload($file,'reimburse');
                $idsArr = explode(',',$ids);
                $date = date('Y-m-d H:i:s');
                $temps = ReimbursePaid::query()
                    ->whereIn('id',$idsArr)
                    ->where('audit_status',2)
                    ->where('paid_status',1)
                    ->pluck('reimburse_no','id');
                $items = [];
                foreach($temps as $_id => $_reimburseNo){
                    $items[$_reimburseNo][] = $_id;
                }
                $flag = false;
                foreach($items as $reimburseNo => $idArr){
                    $flag1 = ReimbursePaid::query()
                        ->whereIn('id',$idArr)
                        ->where('audit_status',2)
                        ->where('paid_status',1)
                        ->update(['paid_status' => 2,'paid_date' => $date,'paid_file' => $path]);
                    if(!$flag1){
                        $flag = false;
                        break;
                    }
                    $detailPaidStatus = ReimbursePaid::query()->where('reimburse_no',$reimburseNo)->pluck('paid_status')->toArray();
                    $paidStatus = ReimbursePaid::extractStatus($detailPaidStatus);
                    $ra = ReimburseApply::query()->where('reimburse_no',$reimburseNo)->first();
                    $ra->paid_status = $paidStatus;
                    $flag2 = $ra->save();
                    if(!$flag2){
                        $flag = false;
                        break;
                    }
                    $rd = ReimburseDetail::query()->where('reimburse_no',$reimburseNo)->first();
                    $rd->paid_status = $paidStatus;
                    $rd->report_date = date('Y-m-01',strtotime($date));
                    $rps = ReimbursePaid::query()
                        ->whereIn('id',$idArr)
                        ->where('paid_status',2)
                        ->get();
                    foreach($rps as $rp){
                        $rd->{'paid_amount_'.$rp->order_by} = $rp->paid_amount;
                        if($rp->order_by != 4){
                            $rd->paid_date = $date;
                        }else{
                            $rd->paid_date_4 = $date;
                            $rd->paid_method_4 = $rp->paid_method;
                            $rd->currency_code_4 = $rp->currency_code;
                            $rd->paid_account_no_4 = $rp->paid_account_no;
                        }
                    }
                    $flag3 = $rd->save();
                    if(!$flag3){
                        $flag = false;
                        break;
                    }
                    $flag = true;
                }
                if(!$flag){
                    DB::rollBack();
                    return response()->json(['status' => false, 'message' => '操作失败！']);
                }else{
                    DB::commit();
                    return response()->json(['status' => true, 'message' => '操作成功！']);
                }
            }else{
                return response()->json(['status' => false, 'message' => '无效文件']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '操作失败！']);
        }
    }
}
